﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ZLCMS.Business;
using NHibernate;
using ZLCMS.Data.Model;
using ZLCMS.Data.Common;
using NHibernate.Criterion;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using System.Collections.Generic;

namespace ZLCMS.Web.SystemManage.Basics
{
    public partial class OrderList : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        public string _isconfirmed;
        public string _paystatus;
        public string _shippingstatus;
        public string _isfinished;
        public string _iscancelled;

        public string _orderno;
        public string _memberid;
        public string _datebegin;
        public string _dateend;
        public string _owner;

        protected void Page_Load(object sender, EventArgs e)
        {
            #region 初始化全局变量值
            //初始化全局变量值
            _isconfirmed = Request["isconfirmed"] == null ? "" : Request["isconfirmed"];
            _paystatus = Request["paystatus"] == null ? "" : Request["paystatus"];
            _shippingstatus = Request["shippingstatus"] == null ? "" : Request["shippingstatus"];
            _isfinished = Request["isfinished"] == null ? "" : Request["isfinished"];
            _iscancelled = Request["iscancelled"] == null ? "" : Request["iscancelled"];

            _orderno = Request["orderno"] == null ? "" : Request["orderno"];
            _memberid = Request["memberid"] == null ? "" : Request["memberid"];
            _datebegin = Request["datebegin"] == null ? "" : Request["datebegin"];
            _dateend = Request["dateend"] == null ? "" : Request["dateend"];
            _owner = UserKit.GetUserAccountFailLogin();
            #endregion

            #region 初始化控件值
            ///初始化控件值
            ParamKit.Bind("OrderIsConfirmed", ddl_isconfirmed, null, new List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, "-1", "-1");
            ddl_isconfirmed.SelectedValue = _isconfirmed;

            ParamKit.Bind("OrderPayStatus", ddl_paystatus, null, new List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, "-1", "-1");
            ddl_paystatus.SelectedValue = _paystatus;

            ParamKit.Bind("OrderShippingStatus", ddl_shippingstatus, null, new List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, "-1", "-1");
            ddl_shippingstatus.SelectedValue = _shippingstatus;

            ParamKit.Bind("OrderIsFinish", ddl_isfinished, null, new List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, "-1", "-1");
            ddl_isfinished.SelectedValue = _isfinished;

            ParamKit.Bind("OrderIsCancel", ddl_iscancelled, null, new List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, "-1", "-1");
            ddl_isfinished.SelectedValue = _iscancelled;

            tb_search_orderno.Text = _orderno;
            tb_search_memberid.Text = _memberid;
            tb_search_datebegin.Text = _datebegin;
            tb_search_dateend.Text = _dateend; 
            #endregion

            //初始化DWZ分页控件
            _framePage = new FrameHelper(this) { RecordCount = GetListCount() };

            BindList(_framePage.PageIndex, _framePage.PageSize);
        }

        #region 填充网格数据 + protected void BindList(int pageIndex, int pageSize)
        /// <summary>
        /// 填充网格数据
        /// </summary>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        protected void BindList(int pageIndex, int pageSize)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<ShopOrderInfo>();
                SetupCondition(ref criteria);
                criteria.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize).AddOrder(Order.Desc("Id"));

                grid.DataSource = criteria.List();
                grid.DataBind();
            }
        } 
        #endregion

        #region 填充查询条件 + private void SetupCondition(ref ICriteria criteria)
        /// <summary>
        /// 填充查询条件
        /// </summary>
        /// <param name="criteria"></param>
        private void SetupCondition(ref ICriteria criteria)
        {
            if (_isconfirmed.Trim() != "-1" && !string.IsNullOrEmpty(_isconfirmed))
            {
                criteria.Add(Expression.Eq("IsConfirmed", _isconfirmed));//是否确认订单
            }

            if (_paystatus.Trim() != "-1" && !string.IsNullOrEmpty(_paystatus))
            {
                criteria.Add(Expression.Eq("PayStatus", _paystatus));//支付状态
            }

            if (_shippingstatus.Trim() != "-1" && !string.IsNullOrEmpty(_shippingstatus))
            {
                criteria.Add(Expression.Eq("ShippingStatus", _shippingstatus));//物流状态
            }

            if (_isfinished.Trim() != "-1" && !string.IsNullOrEmpty(_isfinished))
            {
                criteria.Add(Expression.Eq("IsFinished", _isfinished));//是否完成
            }

            if (_iscancelled.Trim() != "-1" && !string.IsNullOrEmpty(_iscancelled))
            {
                criteria.Add(Expression.Eq("IsCancelled", _iscancelled));//是否取消
            }

            if (!string.IsNullOrEmpty(_orderno))
            {
                criteria.Add(Expression.Like("OrderNo", "%" + _orderno + "%"));
            }
            if (!string.IsNullOrEmpty(_memberid))
            {
                criteria.Add(Expression.Like("MemberAccount", "%" + _memberid + "%"));
            }
            if (!string.IsNullOrEmpty(_datebegin))
            {
                criteria.Add(Expression.Gt("OrderTime", DateTime.Parse(_datebegin)));
            }
            if (!string.IsNullOrEmpty(_dateend))
            {
                criteria.Add(Expression.Lt("OrderTime", DateTime.Parse(_dateend)));
            }
            if (!string.IsNullOrEmpty(_owner))
            {
                criteria.Add(Expression.Eq("OwnerAccount", _owner));
            }
        } 
        #endregion

        #region 查询数据总数 + protected int GetListCount()
        /// </summary>
        /// <returns></returns>
        protected int GetListCount()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<ShopOrderInfo>();
                SetupCondition(ref criteria);
                object obj = criteria.SetProjection(Projections.RowCount()).UniqueResult();
                return int.Parse(obj.ToString());
            }
        } 
        #endregion

        #region 获取会员真实姓名 + protected string GetMemberRealName(object account)
        /// <summary>
        /// 获取会员真实姓名
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        protected string GetMemberRealName(object account)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                MemberInfo member = session.CreateCriteria<MemberInfo>().Add(Expression.Eq("OwnerAccount", _owner)).Add(Expression.Eq("AccountId", account.ToString())).UniqueResult<MemberInfo>();

                return member == null ? "--" : member.MemberName;
            }
        } 
        #endregion
    }
}
